package pers.fulsun;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.TimerTask;
import javax.servlet.http.HttpServletRequest;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import pers.fulsun.framework.manager.AsyncManager;
import pers.fulsun.framework.manager.factory.AsyncFactory;
import pers.fulsun.framework.security.LoginBody;
import pers.fulsun.framework.security.service.TokenService;
import pers.fulsun.framework.web.domain.AjaxResult;
import pers.fulsun.project.system.controller.SysLoginController;
import pers.fulsun.project.system.domain.SysUser;
import pers.fulsun.project.system.mapper.SysUserMapper;
/**
 * <p>
 *
 * @author fulsun
 * @date 8/5/2021
 */
@Api(tags = "临时测试接口")
@RestController
public class TestController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysLoginController sysLoginController;

    @ApiOperation(value = "测试接口标题", notes = "测试接口描述")
    @GetMapping("hello")
    public String hello() {
        return "hello";
    }

    @PostMapping("test/hello")
    public String hello2(@RequestBody Test2 test) {
        return "hello";
    }

    @ApiOperation(value = "创建用户", notes = "创建用户")
    @GetMapping("/test/user1")
    public AjaxResult DBtest(HttpServletRequest request) {
        SysUser record = new SysUser();
        record.setUserName("fulsun");
        record.setNickName("fulsun");
        record.setPassword("123456");
        int insert = sysUserMapper.insert(record);
        return AjaxResult.success("", record);
    }

    @ApiOperation(value = "登录用户", notes = "登录用户fulsun")
    @GetMapping("/test/login")
    public AjaxResult testLogin() {
        LoginBody loginBody = new LoginBody("fulsun", "123456", null, null);
        // 保存数据库
        AsyncManager.me().execute(AsyncFactory.recordOper(new Date()+",登录用户fulsun"));
        return sysLoginController.login(loginBody);
    }

    @ApiOperation(value = "获取用户信息", notes = "根据Token获取用户信息")
    @GetMapping("/test/getInfo")
    public AjaxResult testGetInfo() {
        return sysLoginController.getInfo();
    }

    @GetMapping("/test/aysc")
    public static void ayscTest() {
        long start = System.currentTimeMillis();
        System.out.println("AsyncManagerTest.execute()..." + Thread.currentThread().getName());
        AsyncManager.me().execute(new TimerTask() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "start");
                try {
                    Thread.sleep(2000);
                    System.out.println(Thread.currentThread().getName() + "end");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        AsyncManager.me().execute(new TimerTask() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "start");
                try {
                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName() + "end");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        long end = System.currentTimeMillis();
        System.out.println("总共耗时： " + (end - start) + "ms");
    }

    @GetMapping("/test/param")
    public void testParam(Long num, String vin) {
        System.out.println(num + vin);
    }
}

@Data
class Test2{
    private String user;
    private LocalDateTime date;
}